import { MockMethod } from 'vite-plugin-mock'
import Mock from 'mockjs'

function getData(_req) {
  const info = Mock.mock({
    'list|1000': [
      {
        'id|+1': 1,
        stu_num: '2018010' + '@natural(1000, 9999)',
        name: '@cname',
        password: 123456,
        'sex|1': /男|女/,
        'age|18-60': 18,
        phone: /^(13[0-9]|14[5-7]|15[0-9]|18[0-9])\d{8}$/,
        email: /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
        IDCard: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/,
        birthday: '@date',
        identity: /群众|团员|党员/,
        nation: /汉族|少数民族/,
        'status|': /报考中|未报考/,
        introducter: '小明',
        applyNature: '成人高考',
        studyNature: /专科|本科/,
        studyWay: '@integer(2,3)',
        applyYear: /2020|2021/,
        startYear: 2020,
        endYear: 2022,
        tuitionPaid: '@integer(1,2)',
        registerStatus: '已注册',
        studyStatus: '在籍',
        applySchool: /九江学院|北京大学|浙江大学|清华大学/,
        applyMajor: /软件技术|软件工程|土木工程/,
        applyLevel: /专升本/,
        'applyCost|1-210.2-2': 1,
        'precharge|1-210.2-2': 0,
        isBind: '@boolean( 50%, 50%)',
        censusRegister: '@county(true)',
        presentAddr: '@county(true)',
        detail: '@csentence(5, 25)',
      },
    ],
  })
  info.count = info.list.length
  if (_req.query.id) {
    let _data = []
    for (let i = 0; i < info.count; i++) {
      if (info.list[i].id == parseInt(_req.query.id)) {
        _data.push(info.list[i])
      }
    }
    info.list = _data
    info.count = _data.length
    return info
  }



  let total = info.list.length
  let page = _req.query.page,
    pageSize = _req.query.limit

  if(!page || !pageSize) {
    return info.list
  }

  let start = (page - 1) * pageSize
  let end = page * pageSize > total ? total : page * pageSize

  let mydata = []
  for (let i = start; i < end; i++) {
    mydata.push(info.list[i])
  }
  info.list = mydata
  return info
}

function wait(time) {
  return new Promise(resolve => {
    setTimeout(resolve, time)
  })
}

export default [
  {
    url: '/smart/testStudentList',
    method: 'get',
    response: (req: any) => {
      return {
        code: 20000,
        msg: 'success',
        data: getData(req),
      }
    },
  },
  {
    url: '/smart/testStudentList/delete/:id',
    method: 'delete',
    response: (req: any) => {
      // console.log(req)
      return {
        code: 20000,
        msg: 'success',
        data: req.query,
      }
    },
  },
  {
    url: '/smart/testStudentList/add',
    method: 'post',
    response: (req: any) => {
      // console.log(req)
      return {
        code: 20000,
        msg: 'success',
        data: req.body,
      }
    },
  },
  {
    url: '/smart/testStudentList/edit',
    method: 'put',
    response: (req: any) => {
      // console.log(req)
      return {
        code: 20000,
        msg: 'success',
        data: req.body,
      }
    },
  },
] as MockMethod[]
